<?php
	include('../../../public/bootrap.php');
	$tplPro = new XTemplate('../../view/product/productsimilar.htm');
	$tplPro->assign('baseUrl', $baseUrl);
	
	$condition	 = ' 1=1 ';
	$category_id = _POST('category_id');
	$proName	 = _POST('product_name');
	$price_from	 = _POST('price_from');
	$price_to	 = _POST('price_to');
	$productId	 = _POST('productId');
	$relatedProduct	 = _POST('relatedproduct');
	$limit	= (_POST('limit'))?_POST('limit'):10;
	
	$page=(_POST('pi'))?_POST('pi'):1;
	$offset=($page-1)*$limit;
	if($category_id && $category_id!=-1){
		$condition .= ' AND category_id = '.$category_id;
	}
	if($proName){
		$condition .= ' AND product_name LIKE "%'.str_replace(' ', '%', $proName).'%"';
	}
	if($price_from){
		$condition .= ' AND price >= '.$price_from;
	}
	if($price_to){
		$condition .= ' AND price <= '.$price_to;
	}
	if($productId){
		$productRelate = $db->fetchOne("SELECT related_product FROM tblproduct WHERE product_id = $productId");
		
		$condition .= ' AND product_id != '.$productId;
		//print_r($productRelate);
		/*
		if($productRelate['related_product']){
			$condition .= " AND product_id NOT IN (".$productRelate['related_product'].")";
		}
		*/
		if($productRelate['related_product'])
			$arrRelate = explode(',', $productRelate['related_product']);
	}
	$sql = "SELECT * FROM tblproduct WHERE $condition ";
	$products = $db->fetchAll($sql);
	$totalRow = count($products);
	$pageTotal = floor(($totalRow-1)/$limit)+1;
	
	$sql = "SELECT * FROM tblproduct WHERE $condition LIMIT $offset,$limit";
	
	$products = $db->fetchAll($sql);
	
	foreach ($products as $product) {
		if($arrRelate){
			if(in_array($product['product_id'], $arrRelate)){
				$product['checked'] = 'checked="checked"';
			}
		}
		$sql = "SELECT * FROM tblproduct_img WHERE 	product_id = ".$product['product_id'];
		$images = $db->fetchAll($sql);
		foreach($images as $image){
			if($image['basic_img']==1){
				$product['image'] = "<img src='$baseUrl".$image['img_url']."' title='".$image['img_label']."' width='84px' height='98px' />";
			}
		}
		$sql = "SELECT category_name FROM tblcategory WHERE 	category_id = ".$product['category_id'];
		$category = $db->fetchOne($sql);
		$product['category_id'] = $category['category_name'];
		$product['createddate'] = date("H:i:s d-m-Y", strtotime($product['createddate']));
		$tplPro->insert_loop('similar.list', array('list'=>$product));
	}
	if ( $totalRow == 0 ) {
		$tplPro->parse('similar.norecord');
	}
	
	
	$sltCategoryS = getCombo('tblcategory', 'category_id', 'category_name', 's_category_id', '',$category_id , null, null, 'field-175');
	$tplPro->assign('sltCategoryS', $sltCategoryS);
	$arrLimit = array(
						10=>'10', 
						20=>'20',
						30=>'30',
						40=>'40',
						50=>'50',
						100=>'100',
						200=>'200',
					);
	$sltLimit = getComboNormalf('limit', $arrLimit, $limit, null, 'onchange="onSearchAjax()"');
	$tplPro->assign('sltLimit', $sltLimit);
	$tplPro->assign('page', $page);
	$tplPro->assign('totalRow', $totalRow);
	$tplPro->assign('pageTotal', $pageTotal);
	$tplPro->assign('proName', $proName);
	$tplPro->assign('price_from', $price_from);
	$tplPro->assign('price_to', $price_to);
	$tplPro->parse('similar');
	$content = $tplPro->text('similar');
	$data	= array( "content"=>$content);
	print json_encode($data);
	exit();